﻿public class NetHandler_UpdateRequirement : GameServerMessageHandler
{
    public const string Key = "UpdateRequirement";

    public static NetHandler_UpdateRequirement NewHandler()
    {
        return new NetHandler_UpdateRequirement();
    }

    protected override ISerialize NewData() => new UpdateRequirementRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        UpdateRequirementRequestData requestData = _data as UpdateRequirementRequestData;

        GameDebug.Log($"用户{client.userID} 更新申请:{requestData?.requirement}");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                GameDebug.Log($"{user.data.id} 更新匹配要求");

                user.data.UpdateRequire(requestData.requirement);

                int ret = users.UpdateRequirement(user.data.id, requestData.requirement);


                if (ret >= 0)
                    response.code = 0;
                else
                    response.code = -1;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
